<template>
  <div>
      <div style="margin-bottom: 10px;margin-top: 10px;">
          <el-input v-model="name"  placeholder="请输入机构名称" suffix-icon="el-icon-search" style="width: 200px"
          @keyup.enter.native="loadPost"></el-input>
          <el-select v-model="status" placeholder="请选择状态" style="margin-left: 10px">
           <el-option
            v-for="item in statuss"
            :key="item.value"
            :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <!-- 会员 -->

          <el-select style="margin-left: 10px" v-model="isVip" filterable placeholder="请选择是否为会员">
              <el-option
                      v-for="item in ISvipS"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value">
              </el-option>
          </el-select>
     
          <el-button type="primary" style="margin-left: 5px" @click="loadPost" >查询</el-button>
          <el-button type="success" @click="resetParam">重置</el-button>
          <el-button type="primary" style="margin-left: 5px" @click="add">新增</el-button>
      </div>
  <el-table :data="tableData"
  :header-cell-style="{background:'#f2f5fc',color:'#555'}" border>
      <el-table-column prop="id" label="ID" width="180">
      </el-table-column>

      <el-table-column prop="name" label="机构名称" width="240">
      </el-table-column>
 
      <el-table-column prop="address" label="机构地址"  width="240">
      </el-table-column>

      <el-table-column prop="legalName" label="法人姓名"  width="160">
      </el-table-column>

      <el-table-column prop="registeNum" label="统一社会信用代码"  width="180">
      </el-table-column>

      <el-table-column prop="isVip" label="会员"  width="100" :formatter="isVipFormate">
      </el-table-column>

      <el-table-column prop="status" label="状态"  width="100" :formatter="statusFormate">
      </el-table-column>
      
      <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
          <el-button size="small" type="primary" @click="mod(scope.row)">编辑</el-button>
          
          <el-button size="small" type="success" @click="xq(scope.row.id)">机构详情</el-button>

          <el-popconfirm
                      style="margin-left: 5px;"
                      title="这是一段内容确定删除吗？"
                      @confirm="del(scope.row.id)"
              >
                  <el-button slot="reference" size="small" type="danger">删除</el-button>
              </el-popconfirm>
          </template>
      </el-table-column>

  </el-table>
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pagenum"
              :page-sizes="[2,5, 10, 15, 20,25,30]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>
<!-- 详情 -->

<el-dialog
  title="机构详情"
  :visible.sync="centerDialogVisiblexiangq"
  width="40%"
  center>
  <span>机构名称 <span>{{xsDate.name}}</span></span>
<p>  <span>法人姓名 <span>{{xsDate.legalName}}</span></span></p>
<p>  <span>统一社会信用代码 <span>{{xsDate.registeNum}}</span></span></p>
<p>  <span>机构地址 <span>{{xsDate.address}}</span></span></p>
<p>  <span>法人姓名 <span>{{xsDate.legalName}}</span></span></p>
<p>  <span>营业执照 </span> <img :src="xsDate.licensePath" alt=""></p>
  <span slot="footer" class="dialog-footer">
    <el-button @click="centerDialogVisiblexiangq = false">取 消</el-button>
    <el-button type="primary" @click="centerDialogVisiblexiangq = false">确 定</el-button>
  </span>
</el-dialog>
      <el-dialog
              title="提示"
              :visible.sync="centerDialogVisible"
              width="80%"
              center>
<!--           //增加表单-->
          <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
          
              <el-form-item label="机构名称" prop="name">
                  <el-input v-model="form.name"></el-input>
              </el-form-item>

              <el-form-item label="法人姓名" prop="legalName">
                      <el-input v-model="form.legalName"></el-input>
              </el-form-item>

              <el-form-item label="机构地址" prop="address">
                      <el-input v-model="form.address"></el-input>
              </el-form-item>

              <el-form-item label="统一社会信用代码" prop="registeNum">
                      <el-input  v-model="form.registeNum"></el-input>
              </el-form-item>

             
                      <el-form-item label="会员">
                  <el-select v-model="form.isVip" >
                  <el-option label="会员" value="1"></el-option>
                  <el-option label="非会员" value="0"></el-option>
                   </el-select>
                </el-form-item>
             

              <el-form-item label="营业执照图片" prop="licensePath">
                <el-upload
                     class="avatar-uploader"
                     action="http://localhost:9001/jk/admin/unit/upload"
                     :data={id:id}
                    :show-file-list="false"
                    :on-success="handleAvatarSuccess"
                   >
                        <img v-if="form.licensePath" :src="form.licensePath" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
              </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="save">确 定</el-button>
            </span>


      </el-dialog>


  </div>
</template>

<script>

    export default {
        name: "UnitList",
       
        data() {
         
            return {
                // 查询下拉框
                statuss: [{
          value: -1,
          label: '全部状态'
        }, {
          value: 1,
          label: '申请中'
        }, {
          value: 2,
          label: '审核通过'
        }, {
          value: 3,
          label: '审核未通过'
        }],
        status: -1,
        isVip:-1,

        ISvipS:[{
            value: -1,
            label: '会员+非会员'
            }, {
              value: 1,
              label: '会员'
                },
                {
              value: 2,
              label: '非会员'
                }],

              tableData: [],
              pagenum:1,
              pageSize:2,
              total:0,
              name:'',
              id:'',
              session:'',
              xsDate:[],
              centerDialogVisiblexiangq:false,
                centerDialogVisible:false,
                form:{
                    id:'',
                    name:'',
                    legalName:'',
                    address:'',
                    registeNum:'',
                    isVip:'',
                    licensePath:'',
                    createUserName:'',
                    createUserId:''
                 
                },
                rules: {
                   
                    name: [
                        { required: true, message: '请输入名字', trigger: 'blur' },
                    ],
                   

                }

            }

        },
        created(){
            this.session = JSON.parse(sessionStorage.getItem('CurUser'))
            this.form.createUserId=this.session.createUserId
            this.form.createUserName=this.session.createUserName
           
        },
        methods:{
              // 显示状态
              statusFormate(row, index){
                if(row.status == 1){
                    return "申请中";
                }else if(row.status == 2){
                    return "审核通过";
                }else if(row.status == 3){
                    return "审核未通过";
                }
            },
              // 显示状态
              isVipFormate(row, index){
                if(row.isVip == 2){
                    return "非会员";
                }else if(row.isVip == 1){
                    return "会员";
                }
            },
            resetForm() {
                this.$refs.form.resetFields();
            },
            mod(row){
            //    赋值到表单
                this.centerDialogVisible=true
                this.$nextTick(()=>{
                    this.id=row.id
                    this.form.id = row.id
                    this.form.name = row.name
                    this.form.address = row.address
                    this.form.legalName = row.legalName
                    this.form.registeNum = row.registeNum
                    this.form.isVip = row.isVip
                    this.form.status = row.status
                    this.form.licensePath = row.licensePath
               
                    //    这个在第一次的时候不能使用，所以需要nextTick异步
                })
            },
            // 删除
            del(id){
                this.$axios.delete(this.$httpUrl+'/jk/admin/unit/remove?id='+id).then(res=>res.data).then(res=>{

                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面
                        this.loadPost()
                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })

            },
            xq(){

            },
            add(){

                this.centerDialogVisible=true
                this.$nextTick(()=>{
                    this.resetForm()
                //    这个在第一次的时候不能使用，所以需要nextTick异步
                })
            },
            // 修改
            doMod(){
                this.$axios.put(this.$httpUrl+'/jk/admin/unit/update',
                    this.form

                ).then(res=>res.data).then(res=>{

                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
            // 增加保存
            doSave(){
                this.$axios.post(this.$httpUrl+'/jk/admin/unit/add',
                    this.form

                ).then(res=>res.data).then(res=>{

                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
            save(){
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        if (this.form.id){
                            this.doMod()
                        }else{
                            this.doSave()
                        }

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });

            },
            handleSizeChange(val) {
                
                this.pagenum=1
                this.pageSize=val
                this.loadPost()
            },
            handleCurrentChange(val) {
                
                this.pagenum=val
                this.loadPost()
            },
            handleAvatarSuccess(res, file) {
                this.imageUrl = URL.createObjectURL(file.raw);
            },
          
            resetParam(){
                this.name='',
                this.isVip=-1,
                this.status=-1
                
            },
            loadPost(){
             
                this.$axios.get(this.$httpUrl+'/jk/admin/unit/list?status='+this.status+'&isVip='+this.isVip+'&name='+this.name+'&pagenum='+this.pagenum +'&pageSize='+this.pageSize
                ).then(res=>res.data).then(res=>{
                    console.log(this.isVip)
                  console.log(res)
                    if (res.resCode=='000000'){
                        this.tableData=res.data.records
                        this.total=res.data.total

                    }else{
                        alert('获取数据失败')
                    }

                })
            },
            
            // 详情
            xq(id){
                this.centerDialogVisiblexiangq=true
                
                this.$axios.get(this.$httpUrl+'/jk/admin/unit/detail?id='+id).then(res=>res.data).then(res=>{
                    this.xsDate=res.data



                })
            },
            // 上传文件
            handleAvatarSuccess(res, file) {
        this.form.licensePath = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
     
        },
        beforeMount() {
            
            this.loadPost();

        }
    }
</script>

<style lang="less" scoped>
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }

</style>